// Copyright 2013 The Go Circuit Project
// Use of this source code is governed by the license for
// The Go Circuit Project, found in the LICENSE file.
//
// Authors:
//   2013 Petar Maymounkov <p@gocircuit.org>

// Package focus provides a namespace which can mount and arrange cross-hierarchies, intended to be FUSE-mounted locally
package focus

import (
	"encoding/gob"

	"circuit/kit/kinfolk"
	"circuit/kit/lang"
	"circuit/kit/rh"
	"circuit/kit/rh/ns"
	"circuit/kit/rh/shutter"
	"circuit/use/circuit"
)

// Peer
type Peer struct {
	Kin     kinfolk.KinXID // Cross-references to the kin system at this locus
	Shutter circuit.PermX  // Permanent cross-reference to locus' shared resources rh.Server
}

func (i Peer) Key() string {
	return i.Kin.ID.String()
}

func (i Peer) ID() lang.ReceiverID {
	return i.Kin.ID
}

func init() {
	gob.Register(&Peer{})
}

//
type PeerFunc func() []*Peer

// New creates a new focus namespace
func New(peer *Peer, user string, peers PeerFunc, shutter *shutter.Shutter, shutterFID rh.FID) rh.Session {
	srv := ns.New(focus{
		peer:    peer,
		shutter: shutter,
		cluster: newCluster(peer, peers, shutterFID),
		loci:    newLoci(peer, peers, shutterFID),
	})
	ssn, err := srv.SignIn(user, "")
	if err != nil {
		panic("x\n")
	}
	return ssn
}

// ▒ /
type focus struct {
	peer    *Peer
	cluster ns.Dir
	loci    ns.Dir
	//
	shutter *shutter.Shutter
}

// ▒ /cluster
func (s focus) Cluster() ns.Dir {
	return s.cluster
}

// ▒ /loci
func (s focus) Loci() ns.Dir {
	return s.loci
}

// ▒ /dbg
func (s focus) Dbg() rh.FID {
	return s.shutter.Dbg()
}
